<?php


namespace app\admin\model;

use think\model;
use think\Db;

class ExampleModel extends model
{
    protected $name = 'example';



    // 开启自动写入时间戳字段

    protected $autoWriteTimestamp = true;



    /**

     * [getArticleByWhere 根据搜索条件获取案例列表信息]

     * @param $map

     * @param $Nowpage

     * @param $limits

     * @param $od

     * @return bool|\PDOStatement|string|\think\Collection

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function getExampleByWhere($map, $Nowpage, $limits,$od)

    {

        return $this->alias ('r')

            ->field('r.*,rc.name as cate_name,rd.name as industry_name')

            ->join('example_cate rc', 'r.cate_id = rc.id')

            ->join('example_industry rd', 'r.industry_id = rd.id')

            ->where($map)

            ->page($Nowpage, $limits)

            ->order($od)

            ->select();

    }



    /**

     * [insertLabel 添加标签]

     * @param $selectNoSet

     * @return array

     */

    public function insertLabel($selectNoSet)

    {

        Db::startTrans();// 启动事务

        try{

            foreach ($selectNoSet as $k => $v){

                db('label')->insertAll($v);

            }

            //提交事务

            Db::commit();

            writelog('标签添加成功',200);

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('标签添加失败',100);

        }

    }



    /**

     * [getId 获取标签id]

     * @param $selectNoSet

     * @return array

     */

    public function getId($selectNoSet)

    {

        $id = [];

        Db::startTrans();// 启动事务

        try{

            foreach ($selectNoSet as $k => $v){

                $ids=db('label')->insertGetId($v);

                array_push($id,$ids);

            }

            //提交事务

            Db::commit();

            writelog('标签id获取成功',200);

            return $id;

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('标签id获取失败',100);

            return 555;

        }

    }



    /**

     * [insertArticle 添加案例]

     * @param $param

     * @return array

     */

    public function insertExample($param)

    {

        Db::startTrans();// 启动事务

        try{

            $this->allowField(true)->save($param);

            Db::commit();// 提交事务

            writelog('案例【'.$param['title'].'】添加成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '案例添加成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('案例【'.$param['title'].'】添加失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' =>'案例添加失败'];

        }

    }



    /**

     * [updateArticle 编辑案例]

     * @param $param

     * @return array

     */

    public function updateArticle($param)

    {

        Db::startTrans();// 启动事务

        try{

            $this->allowField(true)->save($param, ['id' => $param['id']]);

            Db::commit();// 提交事务

            writelog('案例【'.$param['title'].'】编辑成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '案例编辑成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('案例【'.$param['title'].'】编辑失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '案例编辑失败'];

        }

    }



    /**

     * [getOneArticle 根据案例id获取一条信息]

     * @param $id

     * @return array|bool|\PDOStatement|string|Model|null

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\ModelNotFoundException

     * @throws \think\exception\DbException

     */

    public function getOneArticle($id)

    {

        return $this->where('id', $id)->find();

    }



    /**

     * [delArticle 删除案例]]

     * @param $id

     * @return array

     */

    public function delExample($id)

    {

        $title = $this->where('id',$id)->value('title');

        Db::startTrans();// 启动事务

        try{

            $this->where('id', $id)->delete();

            Db::commit();// 提交事务

            writelog('案例【'.$title.'】删除成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '案例删除成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('案例【'.$title.'】删除失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' =>  '案例删除失败'];

        }

    }



    /**

     * [batchDelArticle 批量删除案例]

     * @param $param

     * @return array

     */

    public function batchDelArticle($param){

        Db::startTrans();// 启动事务

        try{

            ArticleModel::destroy($param);

            Db::commit();// 提交事务

            writelog('案例批量删除成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量删除成功'];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('案例批量删除失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量删除失败'];

        }

    }



    /**

     * [articleState 案例状态]

     * @param $param

     * @return array

     */

    public function articleState($id,$num){

        $title = $this->where('id',$id)->value('title');

        if($num == 2){

            $msg = '禁用';

        }else{

            $msg = '启用';

        }

        Db::startTrans();// 启动事务

        try{

            $this->where('id',$id)->setField(['status'=>$num]);

            Db::commit();// 提交事务

            writelog('案例【'.$title.'】'.$msg.'成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '已'.$msg];

        }catch( \Exception $e){

            Db::rollback();// 回滚事务

            writelog('案例【'.$title.'】'.$msg.'失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => $msg.'失败'];

        }

    }



    /**

     * [forbiddenArticle 批量禁用案例]

     * @param $param

     * @return array

     */

    public function forbiddenArticle($param){

        Db::startTrans();// 启动事务

        try{

            if($param){

                $this->saveAll($param);

            }else{

                $this->where('1=1')->update(['status'=>2]);

            }

            Db::commit();// 提交事务

            writelog('批量禁用案例成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('批量禁用案例失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量禁用失败'];

        }

    }



    /**

     * [usingArticle 批量启用案例]

     * @param $param

     * @return array

     */

    public function usingArticle($param){

        Db::startTrans();// 启动事务

        try{

            if($param){

                $this->saveAll($param);

            }else{

                $this->where('1=1')->update(['status'=>1]);

            }

            Db::commit();// 提交事务

            writelog('批量启用案例成功',200);

            return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];

        }catch( \Exception $e){

            Db::rollback ();//回滚事务

            writelog('批量启用案例失败',100);

            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量启用失败'];

        }

    }
}